<%@page import="com.tubeonfire.model.admin.ChannelModel"%>
<%@page import="com.tubeonfire.entity.Channel"%>
<%@page import="com.tubeonfire.search.admin.TubeSearchModel"%>
<%@page import="com.tubeonfire.entity.Playlist"%>
<%@page import="com.tubeonfire.model.admin.PlaylistModel"%>
<%@page import="java.util.Locale"%>
<%@page import="java.text.NumberFormat"%>
<%@page import="com.tubeonfire.entity.Tube"%>
<%@page import="com.google.appengine.api.blobstore.BlobKey"%>
<%@page import="com.google.appengine.api.images.ImagesServiceFactory"%>
<%@page import="com.google.appengine.api.images.ImagesService"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	TubeSearchModel model = (TubeSearchModel)request.getAttribute("result");
	String url = (String)request.getAttribute("url");
	List<Tube> list = model.getListResult();
	ImagesService imagesService = ImagesServiceFactory.getImagesService(); 	
	NumberFormat nf = NumberFormat.getNumberInstance(Locale.US); 		
	PlaylistModel plModel = new PlaylistModel();
	plModel.prepareAll(true);
	ChannelModel chModel = new ChannelModel();
	chModel.prepareList();
	String playlistId = (String) request.getAttribute("playlistId");
	String channelId = (String) request.getAttribute("channelId");
	String keyword = (String) request.getAttribute("keyword");
	String strPara = "";	 
	if(playlistId!=null&&playlistId.length()>0){
		strPara += "&playlistId=" + playlistId; 
	}
	if(channelId!=null&&channelId.length()>0){
		strPara += "&channelId=" + channelId; 
	}
	if(keyword!=null&&keyword.length()>0){
		strPara += "&keyword=" + keyword; 
	}	
%>
<jsp:include page="/admin/elements/header.jsp">
	<jsp:param value="Tube Manager" name="title"/>
</jsp:include>
<script type="text/javascript" src="/admin/js/mine/tube_manager.js"></script>
<script type="text/javascript" src="/admin/js/custom/tables.js"></script>
<script type="text/javascript" src="/admin/js/custom/general.js"></script>
	<body class="loggedin">
		<jsp:include page="/admin/elements/top.jsp"/>
	    <!-- START OF MAIN CONTENT -->
	    <div class="mainwrapper">
	     	<div class="mainwrapperinner">  	     		       	       
				<jsp:include page="/admin/elements/left.jsp">
					<jsp:param value="tube" name="current_page"/>
				</jsp:include>					              	                   
	        	<div class="maincontent noright">
	        		<div class="maincontentinner">	
        				<ul class="maintabmenu">
		                	<li><a href="/admin/youtube/search">Youtube Search</a></li>
		                	<li class="current"><a href="/admin/tube/list">Tube Manager</a></li>		
		                	<li><a href="/admin/tube/slideshow">Slide Show Manager</a></li>		
		                </ul><!--maintabmenu-->	            			               
		                     
	                	<div class="content">	
	                	<jsp:include page="/admin/elements/message.jsp"/>	
	                	<div id="messageDiv" class="notification hidden">						    
						    <p></p>
						</div>
	                		<form id="youtubeSearch" class="stdform" method="get" action="/admin/tube/list">
	                			<p>
		                        	<label>By Playlist</label>
		                            <select class="radius3 playlist" id="listCategory" name="playlistId">
				                    	<option value="">All</option>
				                    	<%
				                    		for(Playlist pl : plModel.getListResult()){			                    						                    		
				                    	%>
				                        	<option <%=(playlistId!=null&&playlistId.equals(pl.getId())?"selected":"") %> value="<%=pl.getId()%>"><%=pl.getTitle() %></option>
				                        <%
				                			}
				                        %>
				                    </select> &nbsp;
		                        </p> 
		                        <p>
		                        	<label>By Channel</label>
		                            <select class="radius3 playlist" id="listChannel" name="channelId">
				                    	<option value="">All</option>
				                    	<%
				                    		for(Channel ch : chModel.getListResult()){			                    						                    		
				                    	%>
				                        	<option <%=(channelId!=null&&channelId.equals(ch.getId())?"selected":"") %> value="<%=ch.getId()%>"><%=ch.getTitle() %></option>
				                        <%
				                			}
				                        %>
				                    </select> &nbsp;
		                        </p>     		                         
		                    	<p>
		                        	<label>Keyword</label>
		                            <span class="field">
		                            	<input type="text" style="font-style: italic;" name="keyword" id="formKeyword" class="mediuminput" value="<%=keyword!=null?keyword:""%>">
		                            </span>
		                        </p>                                                                        		                                                                    		                  
		                        <p class="stdformbutton">
		                        	<button class="submit radius2" id="btnSubmitList">Search</button>		                        	
		                        </p>
                   			</form>	 	                		                	   	                    
	                    	<div class="contenttitle">
	                    		<h2 class="image"><span>List Tube</span></h2>
	                    	</div>	                    	
			                <div class="tableoptions">			                	
			                    <select class="radius3 action" id="action">			  
			                    	<option value="0">Select action</option>                  	
			                    	<option value="1">Delete all checked tube</option>	
			                    	<option value="2">Show all checked tube in slideshow</option>	
			                    	<%
			                    		for(Playlist pl : plModel.getListResult()){			                    						                    		
			                    	%>
			                        	<option value="<%=pl.getId()%>" title="<%=pl.getTitle() %>">Change to playlist '<%=pl.getTitle() %>'</option>
			                        <%
			                			}
			                        %>		                    	
			                    </select> &nbsp;
			                    <button class="radius3" id="applyTube">Apply</button>			                  
			                    <div class="desc tooltips" style="float: right;">
			                    	Tips			                    	
			                    </div>
			                </div>						                          
		                    <table id="table-result" cellpadding="0" cellspacing="0" border="0" class="stdtable stdtablecb">		                    
			                    <colgroup>
			                        <col class="con0">
			                        <col class="con1">
			                        <col class="con0">
			                        <col class="con1">
			                        <col class="con0">
			                        <col class="con1">
			                        <col class="con0">
			                    </colgroup>
			                    <thead>
			                        <tr>
			                        	<th class="head0"><span class="checkbox"><input type="checkbox" class="checkall"></span></th>
			                            <th class="head1">Thumb</th>
			                            <th class="head0">Title</th>
			                            <th class="head1">Playlist</th>
			                            <th class="head0">Duration</th>
			                            <th class="head1">Views</th>
			                          	<th class="head1">Action</th>
			                        </tr>
			                    </thead>
			                    <tfoot>
			                        <tr>
			                        	<th class="head0"><span class="checkbox"><input type="checkbox" class="checkall"></span></th>
			                            <th class="head1">Thumb</th>
			                            <th class="head0">Title</th>
			                             <th class="head1">Playlist</th>
			                            <th class="head0">Duration</th>
			                            <th class="head1">Views</th>
			                          	<th class="head1">Action</th>
			                        </tr>
			                    </tfoot>
			                    <tbody>
			                    <%
				                	if(list!=null&&list.size()>0){				               								
										for(Tube tube: list){
											Playlist pl = PlaylistModel.byId(tube.getPlaylistId(), true);
											String imgUrl = tube.getImageUrl();
											if(!tube.getImageUrl().contains("http://")){
												try{
													BlobKey blobKey = new BlobKey(imgUrl);
													imgUrl = imagesService.getServingUrl(blobKey);
												}catch(Exception e){
													imgUrl = "/admin/images/tube.png";
												}														
											}											
									%>
				                        <tr>
				                        	<td class="center"><span class="checkbox"><input type="checkbox" value="<%=tube.getId()%>"></span></td>
				                            <td class="center">
				                            	<img src="<%=imgUrl %>" alt="" width="100px">
				                            </td>
				                            <td><a href="/video/<%=tube.getId()%>/<%=tube.getAlias()%>.html" target="blank" title="View this tube"><%=tube.getTitle() %></a></td>
				                            <td class="center playlist-link">
				                            	<%
				                            		if(pl!=null){
				                            		%>
				                            			<a target="blank" href="/admin/playlist/edit?id=<%=pl.getId() %>" title="View <%=pl.getTitle() %> playlist"><%=pl.getTitle() %></a>				                            			
				                            		<%
				                            		}else{
				                            			%>
				                            			<small>Not exists</small>  
				                            			<%
				                            		}
				                            	%>				                            	
			                            	</td>
				                            <td class="center"><%=tube.getTotalTime() %></td>
				                            <td class="center"><%=nf.format(tube.getView())%></td>				                           
				                            <td class="center" width="280px">
				                            	<a class="btnBump stdbtn" target="blank" href="<%=tube.getId()%>" title="This tube will be the first one in playlist">Bump</a>
				                            	<a class="btnBanner stdbtn" target="blank" href="<%=tube.getId()%>" title="Show this tube in home page slideshow">Slideshow</a>				                            	 				                            
				                            	<a class="stdbtn <%=pl!=null?"":"btn_red" %>" target="blank" href="/admin/tube/edit?id=<%=tube.getId()%>" id="<%=tube.getId()%>" title="Edit tube information">Edit</a> 
				                            	<a class="btnDelete stdbtn" target="blank" href="<%=tube.getId()%>" title="Delete this tube">Delete</a>				                            	
				                            </td>
				                        </tr>
			                        <%
										}	
				                	}
			                        %>			                        		                       
			                    </tbody>
			                </table>   		                   
	                    	                 	
	                    	<div class="dataTables_paginate paging_full_numbers" id="dyntable_paginate" style="border: none">
	                    		<%
	                    			int showing = (model.getPage()-1)*model.getLimit() + 1;
	                    			int to = ((model.getPage()-1)*model.getLimit()) + model.getListResult().size();
	                    			if(to==0){
	                    				showing = 0;
	                    			}
	                    		%>                    	
	                    		<span style="float: left;">Showing <b><%=showing %></b> to <b><%=to %></b> of <b><%=nf.format(model.getTotalResult()) %></b> tubes</span>
	                    	<%
		                    	if(model.getTotalPage()>1){		                    
		                    		if(model.getPage()>1){
	                    				%>
	                    				<a href="<%=url%>?page=1<%=strPara%>"><span class="first paginate_button paginate_button_disabled">First</span></a>
	                    				<a href="<%=url%>?page=<%=model.getPage()-1%><%=strPara%>"><span class="next paginate_button">Previous</span></a>
	                    				<%
	                    			}
	                    		%>
	                    		<span>
	                    		<%
		                    		int startPage = 1;
	                    			int endPage = 7;
	                    			int currentPage = model.getPage();
	                    			int totalPage = model.getTotalPage();
	                    			if(currentPage<=3){
	                    				startPage = 1;
	                    				if(totalPage >= 7){
	                    					endPage = 7;	
	                    				}else{
	                    					endPage = totalPage;
	                    				}	                    					
	                    			}else{	
	                    				startPage = currentPage - 3;
	                    				if((totalPage-currentPage)>3){
	                    					endPage = currentPage + 3;	
	                    				}else{
	                    					endPage = totalPage;
	                    					startPage = totalPage - 6;
	                    					if(startPage<=0){
	                    						startPage = 1;
	                    					}	                    					
	                    				}		                    			
	                    			}	 
	                    			for(int i = startPage;i<=endPage;i++){
			                  			if(model.getPage()==i){
			                  				%>  
				                    		<span class="paginate_active" title="<%=i%>"><%=i %></span>
	                    					<%
			                  			}else{
			                  				%>
			                  				<a href="<%=url%>?page=<%=i%><%=strPara%>"><span class="paginate_button"><%=i %></span></a>
			                  				<%
			                  			}
			                  		}	                    			
	                    		%>
	                    		</span>
	                    		<%
		                    		if(model.getTotalPage()>model.getPage()){
	                    				%>
	                    				<a href="<%=url%>?page=<%=model.getPage()+1%><%=strPara%>"><span class="next paginate_button">Next</span></a>
	                    				<a href="<%=url%>?page=<%=model.getTotalPage()%><%=strPara%>"><span class="last paginate_button">Last</span></a>
	                    				<%
	                    			}	                    		
	                    		}		                	
	                    	%>	
                    		</div>               	              
	                    	
	                </div>
	               
	                <!--content-->	                
	            </div><!--maincontentinner-->
				<jsp:include page="/admin/elements/bottom.jsp"></jsp:include>                    	           	
	        </div><!--maincontent-->	                        
	     	</div><!--mainwrapperinner-->
	    </div><!--mainwrapper-->
	    <div id="opacity"></div>
		<img id="image_loadder" src="/admin/images/ajax-loader.gif">   
		<!-- END OF MAIN CONTENT -->    
	</body>
</html>
